package com.nike.amoeba.bean.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.nike.amoeba.bean.db.UploadOrderBean
import com.nike.amoeba.bean.db.StockEpcBean
import java.nio.file.Files.find

/**
 * @Author nike
 * @Date 2023/9/6 17:42
 * @Description
 */
@Dao
interface UploadOrderDao {

    /**
     * 增加
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun add(vararg bean: UploadOrderBean)

    /**
     * 根据用户ID删除全部
     */
    @Query("DELETE FROM UploadOrderBean WHERE userId = :userId AND companyId = :companyId")
    suspend fun deleteById(userId: String, companyId: String)

    /**
     * 根据用户查询所有
     */
    @Query("SELECT * FROM UploadOrderBean WHERE userId = :userId AND companyId = :companyId")
    fun findAll(userId: String?, companyId: String?): List<UploadOrderBean>

    /**
     * 根据订单查询所有
     */
    @Query("SELECT * FROM UploadOrderBean WHERE userId = :userId AND companyId = :companyId AND orderId = :orderId")
    fun findOrderId(orderId: String?, userId: String?, companyId: String?): UploadOrderBean

    //删除数据库表全部数据
    @Query("delete from UploadOrderBean")
    suspend fun deleteAll()
    /**
     * 查一个
     */
    @Query("SELECT * FROM UploadOrderBean WHERE orderId =:id")
    fun find(id: String): UploadOrderBean


    /**
     * 更新
     */
    @Update
     fun update(bean: UploadOrderBean)

    suspend fun insertOrUpdate(bean: UploadOrderBean) {
        val find = find(bean.orderId!!)
        if (find == null){
            add(bean)
        }else{
            update(bean)
        }
    }

}